{% extends "base.html" %}
{% load staticfiles %}

{% block title %}当日查询{% endblock %}

{% block content %}
    <!-- Main content -->
    <body>
    <section class="content">
        <div class="nav-tabs-custom">
            <ul class="nav nav-tabs">
                <li class="active" style="margin-top:10px"><a href="#fa-order" onclick="tabs(0)"
                                                              data-toggle="tab">订单查询</a></li>
                <li style="margin-top:10px"><a href="#fa-deal" onclick="tabs(1)" data-toggle="tab">成交查询</a></li>
                <li style="margin-top:10px"><a href="#fa-position" onclick="tabs(2)" data-toggle="tab">持仓查询</a></li>
                <li style="margin-top:10px"><a href="#fa-querysearch" onclick="tabs(3)" data-toggle="tab">录入查询</a></li>
            </ul>
            <div class="tab-content">
                <!--订单-->
                <div class="tab-pane active" id="fa-order">
                    <div>
                        <div class="div-query">
                            <div class="order-query"><span>环境 </span>
                                <select class="envir-input-query" id="or_pl_envid">
                                    <option value="1">仿真环境</option>
                                    <option value="2">实盘环境</option>
                                </select></div>
                            <div class="order-query"><span>债券代码 </span>
                                <input class="instrid-input-query" type="text" id="or_instrumentid">
                            </div>
                            <div class="order-query"><span>方向 </span>
                                <select class="direction-input-query" id="or_direction">
                                    <option value=""></option>
                                    <option value="0">Bid</option>
                                    <option value="1">Ofr</option>
                                </select></div>
                            <div class="order-query"><span>清算速度 </span>
                                <select class="tid-input-query" id="or_legsettltype">
                                    <option value=""></option>
                                    <option value="1">T+0</option>
                                    <option value="2">T+1</option>
                                </select></div>
                            <div class="order-query"><span>STC订单号 </span>
                                <input class="input-query" type="text" id="or_ordersysid">
                            </div>
                            <div class="order-query"><span>量化订单号 </span>
                                <input class="input-query" type="text" id="or_orderinnerid">
                            </div>
                            <div class="order-query"><span>策略ID </span>
                                <input class="id-input-query" type="text" id="or_ruleid">
                            </div>
                            <div class="order-query">
                                <button class="btn-query" onclick="searchOrder_()">查询
                                </button>
                            </div>
                        </div>
                        <table class="table table-bordered table-striped table-hover" id="OrderTable">
                        </table>
                    </div>
                </div>
                <!--成交-->
                <div class="tab-pane" id="fa-deal">
                    <div>
                        <div class="div-query">
                            <div class="lbl-query"><span>环境 </span>
                                <select class="envir-input-query" id="tr_pl_envid">
                                    <option value="1">仿真环境</option>
                                    <option value="2">实盘环境</option>
                                </select>
                            </div>
                            <div class="lbl-query"><span>债券代码 </span>
                                <input class="instrid-input-query" type="text" id="tr_instrumentid">
                            </div>
                            <div class="lbl-query"><span>方向 </span>
                                <select class="direction-input-query" id="tr_direction">
                                    <option value=""></option>
                                    <option value="0">Bid</option>
                                    <option value="1">Ofr</option>
                                </select></div>
                            <div class="lbl-query"><span>清算速度 </span>
                                <select class="tid-input-query" id="tr_legsettltype">
                                    <option value=""></option>
                                    <option value="1">T+0</option>
                                    <option value="2">T+1</option>
                                </select></div>
                            <div class="lbl-query"><span>成交编号 </span>
                                <input class="input-query" type="text" id="tr_tradeid">
                            </div>
                            <div class="lbl-query"><span>策略ID </span>
                                <input class="id-input-query" type="text" id="tr_ruleid">
                            </div>
                            <div class="lbl-query">
                                <button class="btn-query" onclick="searchTrade_()">查询
                                </button>
                            </div>
                        </div>
                        <table class="table table-bordered table-striped table-hover" id="TradeTable">
                        </table>
                    </div>
                </div>
                <!--持仓-->
                <div class="tab-pane" id="fa-position">
                    <div>
                        <div class="div-query">
                            <div class="lbl-query"><span>环境 </span>
                                <select class="envir-input-query" id="po_pl_envid">
                                    <option value="1">仿真环境</option>
                                    <option value="2">实盘环境</option>
                                </select></div>
                            <div class="lbl-query"><span>策略ID </span>
                                <input class="id-input-query" type="text" id="po_ruleid">
                            </div>
                            <div class="lbl-query"><span>策略名称 </span><input type="text" id="po_stgname">
                            </div>
                            <div class="lbl-query"><span>债券代码 </span>
                                <input class="input-query" type="text" id="po_instrumentid">
                            </div>
                            <div class="lbl-query">
                                <button class="btn-query" onclick="searchPosition_()">查询
                                </button>
                            </div>
                        </div>
                        <table class="table table-bordered table-striped table-hover" id="PositionTable">
                        </table>
                    </div>
                </div>
                <!--录入查询-->
                <div class="tab-pane" id="fa-querysearch">
                    <div>
                        <table id="toolbar"></table>
                        <div class="div-query">
                            <div class="lbl-query"><span>环境 </span>
                                <select class="envir-input-query" id="mn_pl_envid">
                                    <option value="1">仿真环境</option>
                                    <option value="2">实盘环境</option>
                                </select>
                            </div>
                            <div class="lbl-query"><span>策略ID </span>
                                <input class="id-input-query" type="text" id="mstgid">
                            </div>
                            <div class="lbl-query">
                                <span>债券代码 </span>
                                <input class="instrid-input-query" type="text" id="mbondid">
                            </div>
                            <div class="lbl-query">
                                <span>对手交易员 </span>
                                <input class="instrid-input-query" type="text" id="mcustomerid">
                            </div>
                            <div class="lbl-query">
                                <span>方向 </span>
                                <select class="direction-input-query" id="mdirection">
                                    <option value=""></option>
                                    <option value="1">Ofr</option>
                                    <option value="0">Bid</option>
                                </select>
                            </div>
                            <div class="lbl-query">
                                <span>清算速度 </span>
                                <select class="tid-input-query" id="qliquidate">
                                    <option value=""></option>
                                    <option value="1">T+0</option>
                                    <option value="2">T+1</option>
                                </select>
                            </div>
                            <div class="lbl-query">
                                <button class="btn-query" onclick="searchInput_()">查询
                                </button>
                            </div>
                        </div>
                        <table class="table table-bordered table-striped table-hover" id="InputTable">
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </section>
    </body>
{% endblock %}

{% block scripts %}


    <script type="text/javascript">

        //页面加载完成后执行
        $(function () {
            searchOrder_()
        })

        function tabs(n) {
            if (n == 0) {
                searchOrder_();
                $('#fa-order').addClass('active');
                $('#fa-deal').removeClass('active');
                $('#fa-position').removeClass('active');
                $('#fa-querysearch').removeClass('active');
            } else if (n == 1) {
                searchTrade_();
                $('#fa-order').removeClass('active');
                $('#fa-deal').addClass('active');
                $('#fa-position').removeClass('active');
                $('#fa-querysearch').removeClass('active');
            } else if (n == 2) {
                searchPosition_();
                $('#fa-order').removeClass('active');
                $('#fa-deal').removeClass('active');
                $('#fa-position').addClass('active');
                $('#fa-querysearch').removeClass('active');
            } else {
                searchInput_();
                $('#fa-order').removeClass('active');
                $('#fa-deal').removeClass('active');
                $('#fa-position').removeClass('active');
                $('#fa-querysearch').addClass('active');
            }
        }

        //搜索 订单
        function searchOrder_() {
            var or_instrumentid = $("#or_instrumentid").val();
            var or_direction = $("#or_direction").val();
            var or_legsettltype = $("#or_legsettltype").val();
            var or_ordersysid = $("#or_ordersysid").val();
            var or_orderinnerid = $("#or_orderinnerid").val();
            var or_ruleid = $("#or_ruleid").val();
            var or_pl_envid = $("#or_pl_envid").val();
            // 整理发送后台的数据
            var data = {
                "pl_envid": or_pl_envid,
                "instrumentid": or_instrumentid,
                "direction": or_direction,
                "legsettltype": or_legsettltype,
                "orderinnerid": or_orderinnerid,
                "ordersysid": or_ordersysid,
                "ruleid": or_ruleid,
            };

            var JsonData = JSON.stringify(data);
            $.post("{% url 'trade:query' %}", {"data": JsonData, 'daySearchOrder_search': 1}, function (r) {
                $("#OrderTable").bootstrapTable("destroy").bootstrapTable({
                    height: 600,
                    columns: [
                        {
                            field: 'orderinnerid',
                            title: '量化订单号',
                        },
                        {
                            field: 'ordersysid',
                            title: 'STC订单号'
                        }, {
                            field: 'orderstatus',
                            title: '状态'
                        }, {
                            field: 'ruleid',
                            title: '策略ID'
                        }, {
                            field: 'rulename',
                            title: '策略名称'
                        }, {
                            field: 'direction',
                            title: '方向'
                        }, {
                            field: 'instrumentid',
                            title: '代码',
                        },
                        {
                            field: 'legsettltype',
                            title: '清算速度',
                        },
                        {
                            field: 'ytm',
                            title: '报价(%)',
                            formatter: function (value) {
                                var inp_split = value.toString().split(".");
                                if (inp_split.length === 1) {
                                    return value + ".0000"
                                } else {
                                    var l = inp_split[1].length
                                    var a = 4 - l;
                                    if (a > 0 && a !== 0) {
                                        for (let i = 0; i < a; i++) {
                                            value += "0"
                                        }
                                    }
                                    return value
                                }
                            }
                        },
                        {
                            field: 'price',
                            title: '净价（元）',
                            formatter: function (value) {
                                if (value === 0) {
                                    return ""
                                } else {
                                    var inp_split = value.toString().split(".");
                                    if (inp_split.length === 1) {
                                        return value + ".0000"
                                    } else {
                                        var l = inp_split[1].length
                                        var a = 4 - l;
                                        if (a > 0 && a !== 0) {
                                            for (let i = 0; i < a; i++) {
                                                value += "0"
                                            }
                                        }
                                        return value
                                    }
                                }
                            }
                        },
                        {
                            field: 'volume',
                            title: '报量(万元)',
                            formatter: function (value) {
                                if (value === 0) {
                                    return ""
                                } else {
                                    return value
                                }
                            }
                        },
                        {
                            field: 'volumetraded',
                            title: '已成交(万元)',
                            formatter: function (value) {
                                if (value === 0) {
                                    return 0
                                } else {
                                    return value
                                }
                            }

                        },
                        {
                            field: 'reserver1',
                            title: '撮合范围',
                        },
                        {
                            field: 'reserver2',
                            title: '渠道',
                            width: 130,
                            formatter: function (value, row, index) {
                                if (value === "2") {
                                    return "默认+" + row.reserver3 + "家"
                                } else if (value === "3") {
                                    if (row.reserver4 && row.reserver4.indexOf(",")!==-1) {
                                        var value_arry = row.reserver4.split(",")
                                        for (let i = 0; i < value_arry.length; i++) {
                                            if (value_arry[i].endsWith("TPSC")) {
                                                value_arry[i] = "国利"
                                            } else if (value_arry[i].endsWith("CTTJ")) {
                                                value_arry[i] = "信唐"
                                            } else if (value_arry[i].endsWith("CNEX")) {
                                                value_arry[i] = "国际"
                                            } else if (value_arry[i].endsWith("CCTB")) {
                                                value_arry[i] = "中诚"
                                            } else if (value_arry[i].endsWith("PICN")) {
                                                value_arry[i] = "平安"
                                            } else if (value_arry[i].endsWith("ODM")) {
                                                value_arry[i] = "ODM"
                                            }
                                        }
                                        return "指定 "+value_arry.toString()
                                    } else if(row.reserver4) {
                                        var val = row.reserver4

                                        if (val.endsWith("TPSC")) {
                                                val = "国利"
                                            } else if (val.endsWith("CTTJ")) {
                                                val = "信唐"
                                            } else if (val.endsWith("CNEX")) {
                                                val = "国际"
                                            } else if (val.endsWith("CCTB")) {
                                                val = "中诚"
                                            } else if (val.endsWith("PICN")) {
                                                val = "平安"
                                            } else if (val.endsWith("ODM")) {
                                                val = "ODM"
                                            }
                                        console.log(3, val)
                                        return "指定["+val+"]"
                                    }
                                } else {
                                    return value
                                }
                            }
                        },
                        {
                            field: 'updatetime',
                            title: '订单更新时间',
                        },
                    ],
                    data: r,
                    {#height: 600,#}
                    //******前端分页设置****
                    pagination: true,
                    pageNumber: 1,
                    pageSize: 20,
                    pageList: [10, 20, 50, 200],
                    paginationHAlign: "right",
                    paginationDetailHAlign: "left"
                    //******前端分页设置****
                })

            });
        }

        //搜索 成交
        function searchTrade_() {
            var tr_instrumentid = $("#tr_instrumentid").val();
            var tr_direction = $("#tr_direction").val();
            var tr_legsettltype = $("#tr_legsettltype").val();
            var tr_tradeid = $("#tr_tradeid").val();
            var tr_ruleid = $("#tr_ruleid").val();
            var tr_pl_envid = $("#tr_pl_envid").val();
            // 整理发送后台的数据
            var data = {
                "pl_envid": tr_pl_envid,
                "instrumentid": tr_instrumentid,
                "direction": tr_direction,
                "legsettltype": tr_legsettltype,
                "tradeid": tr_tradeid,
                "ruleid": tr_ruleid,
            };

            var JsonData = JSON.stringify(data);
            $.post("{% url 'trade:query' %}", {"data": JsonData, 'daySearchTrade_search': 1}, function (r) {
                $("#TradeTable").bootstrapTable("destroy").bootstrapTable({
                    height: 700,
                    data: r,
                    columns: [
                        {
                            field: 'tradeid',
                            title: '成交编号',
                        },
                        {
                            field: 'ruleid',
                            title: '策略ID',
                        }, {
                            field: 'rulename',
                            title: '策略名称',
                        }, {
                            field: 'instrumentid',
                            title: '代码',
                        }, {
                            field: 'direction',
                            title: '方向',
                        }, {
                            field: 'ytm',
                            title: '成交价（%）',
                            formatter: function (value) {
                                if (value === 0) {
                                    return ""
                                } else {
                                    var inp_split = value.toString().split(".");
                                    if (inp_split.length === 1) {
                                        return value + ".0000"
                                    } else {
                                        var l = inp_split[1].length
                                        var a = 4 - l;
                                        if (a > 0 && a !== 0) {
                                            for (let i = 0; i < a; i++) {
                                                value += "0"
                                            }
                                        }
                                        return value
                                    }
                                }
                            }
                        },
                        {
                            field: 'tradeprice',
                            title: '净价（元）',
                            formatter: function (value) {
                                if (value === 0) {
                                    return ""
                                } else {
                                    var inp_split = value.toString().split(".");
                                    if (inp_split.length === 1) {
                                        return value + ".0000"
                                    } else {
                                        var l = inp_split[1].length
                                        var a = 4 - l;
                                        if (a > 0 && a !== 0) {
                                            for (let i = 0; i < a; i++) {
                                                value += "0"
                                            }
                                        }
                                        return value
                                    }
                                }
                            }
                        },
                        {
                            field: 'legsettltype',
                            title: '清算速度'
                        },
                        {
                            field: 'volumetraded',
                            title: '成交量(万)'
                        },
                        {
                            field: 'brokerid',
                            title: '对手方',
                        },
                        {
                            field: 'clientid',
                            title: '对手交易员',
                        },
                        {
                            field: 'commodityid',
                            title: '渠道',
                        },
                        {
                            field: 'investorid',
                            title: '本方交易员',
                        },
                        {
                            field: 'tradetime',
                            title: '成交时间',
                        },
                        {
                            field: 'ordersysid',
                            title: 'STC订单号',
                        },
                        {
                            field: 'orderinnerid',
                            title: '量化订单号',
                        },
                    ],
                    {#height: 600,#}
                    {#width: 100,#}
                    //******前端分页设置****
                    pagination: true,
                    pageNumber: 1,
                    pageSize: 20,
                    pageList: [10, 20, 50, 200],
                    paginationHAlign: "right",
                    paginationDetailHAlign: "left"
                    //******前端分页设置****
                })

            });
        }

        //搜索 持仓
        function searchPosition_() {
            var po_ruleid = $("#po_ruleid").val();
            var po_stgname = $("#po_stgname").val();
            var po_instrumentid = $("#po_instrumentid").val();
            var po_pl_envid = $("#po_pl_envid").val();
            // 整理发送后台的数据
            var data = {
                "pl_envid": po_pl_envid,
                "ruleid": po_ruleid,
                "instrumentid": po_instrumentid,
                "stgname": po_stgname,
            };

            var JsonData = JSON.stringify(data);
            $.post("{% url 'trade:query' %}", {"data": JsonData, 'daySearchPosition_search': 1}, function (r) {
                $("#PositionTable").bootstrapTable("destroy").bootstrapTable({
                    //导出功能设置
                    exportTypes: ['excel'],
                    showExport: true,
                    exportDataType: "all",
                    exportOptions: {
                        fileName: "dayposition"
                    },
                    height: 600,
                    columns: [
                        {
                            field: 'ruleid',
                            title: '策略ID'
                        }, {
                            field: 'rulename',
                            title: '策略名称'
                        }, {
                            field: 'instrumentid',
                            title: '债券代码',
                        },
                        {
                            field: 'position',
                            title: '持仓量（万）',
                            formatter: function (value) {
                                if (value > 0) {
                                    return "+" + value.toString()
                                } else {
                                    return value
                                }
                            }
                        },
                    ],
                    data: r,
                    {#height: 600,#}
                    {#width: 100,#}
                    //******前端分页设置****
                    pagination: true,
                    pageNumber: 1,
                    pageSize: 20,
                    pageList: [10, 20, 50, 200],
                    paginationHAlign: "right",
                    paginationDetailHAlign: "left"
                    //******前端分页设置****
                })

            });
        }


        //搜索 录入
        function searchInput_() {
            var envid = $("#mn_pl_envid").val();
            var stgid = $("#mstgid").val();
            var bondid = $("#mbondid").val();
            var customerid = $("#mcustomerid").val();
            var direction = $("#mdirection").val();
            var settletype = $("#qliquidate").val();
            // 整理发送后台的数据
            var data = {
                "envid": envid,
                "stgid": stgid,
                "bondid": bondid,
                "customerid": customerid,
                "direction": direction,
                "settletype": settletype
            };

            var JsonData = JSON.stringify(data);
            $.post("{% url 'trade:query' %}", {"data": JsonData, 'daySearchInput_search': 1}, function (r) {
                $("#InputTable").bootstrapTable("destroy").bootstrapTable({
                    height: 600,
                    columns: [
                        {
                            field: 'ruleid',
                            title: '策略ID'
                        }, {
                            field: 'stgname',
                            title: '策略名称'
                        }, {
                            field: 'bondid',
                            title: '债券代码'
                        }, {
                            field: 'direction',
                            title: '方向',
                        }, {
                            field: 'ytm',
                            title: '成交价(%)',
                            formatter: function (value) {
                                var inp_split = value.toString().split(".");
                                if (inp_split.length === 1) {
                                    return value + ".0000"
                                } else {
                                    var l = inp_split[1].length
                                    var a = 4 - l;
                                    if (a > 0 && a !== 0) {
                                        for (let i = 0; i < a; i++) {
                                            value += "0"
                                        }
                                    }
                                    return value
                                }
                            }
                        }, {
                            field: 'netprice',
                            title: '净价（元）',
                            formatter: function (value) {
                                if (value === 0) {
                                    return ""
                                } else {
                                    var inp_split = value.toString().split(".");
                                    if (inp_split.length === 1) {
                                        return value + ".0000"
                                    } else {
                                        var l = inp_split[1].length
                                        var a = 4 - l;
                                        if (a > 0 && a !== 0) {
                                            for (let i = 0; i < a; i++) {
                                                value += "0"
                                            }
                                        }
                                        return value
                                    }
                                }
                            }
                        }, {
                            title: '清算速度',
                            field: 'settletype'
                        },
                        {
                            title: '成交量（万）',
                            field: 'volume'
                        },
                        {
                            title: '对手交易员',
                            field: 'customerid'
                        },
                        {
                            title: '录入时间',
                            field: 'transactime'
                        }
                    ],
                    data: r,
                    {#height: 600,#}
                    {#width: 100,#}
                    //******前端分页设置****
                    pagination: true,
                    pageNumber: 1,
                    pageSize: 20,
                    pageList: [10, 20, 50, 200],
                    paginationHAlign: "right",
                    paginationDetailHAlign: "left"
                    //******前端分页设置****
                })

            });
        }


    </script>
    <style>
        {#加载数据时的样式#}
        .bootstrap-table .fixed-table-container .fixed-table-body .fixed-table-loading {
            align-items: center;
            background: #141414;
            display: none;
            justify-content: center;
            position: absolute;
            bottom: 0;
            width: 100%;
            z-index: 1000;
        }

        .bootstrap-table .fixed-table-container .fixed-table-body {
            overflow-x: auto;
            overflow-y: auto;
            height: 100%;
            /*margin-top: 43px;*/
        }

    </style>

{% endblock %}